package com.woniu.supermarket.web;

import com.github.pagehelper.PageInfo;
import com.woniu.supermarket.entity.Category;
import com.woniu.supermarket.result.ResultResponse;
import com.woniu.supermarket.service.ICategoryService;
import com.woniu.supermarket.vo.CategoryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("category")
@Api(value = "类别目录的操作")
public class CategoryController {

    @Autowired
    ICategoryService iCategoryService;

    //分页查询
    @GetMapping("/clist")
    @ApiOperation(value = "分页查询类别目录")
    public ResultResponse<PageInfo<CategoryVo>> clist(
            @RequestParam(value = "now",defaultValue = "1") Integer now,
            @RequestParam(value = "size",defaultValue = "5")Integer size,
            @RequestParam(value = "cons",defaultValue = "") String cons){
        ResultResponse<PageInfo<CategoryVo>> result = null;

        try {
            PageInfo<CategoryVo> pageInfo = iCategoryService.queryByPage(now, size, cons);
            result = new ResultResponse<>(200,"查询类别目录成功!",pageInfo);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询类别目录失败!");
        }
        return result ;
    }
    

    @PostMapping("/add")
    @ApiOperation(value = "添加类别")
    public ResultResponse<Void> add(@RequestBody Category category){
        ResultResponse<Void> result = null;

        try {
            iCategoryService.add(category);
            result = new ResultResponse<>(200,"添加类别成功!");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"添加类别失败!");
        }
        return result ;
    }

    @DeleteMapping("/del/{id}")
    @ApiOperation(value = "根据id删除类别")
    public ResultResponse<Void> del(@PathVariable("id") int id){
        ResultResponse<Void> result = null;

        try {
            //判断id查出来的数据的parent_id,是否为-1
            //-1则直接删除
            //不是-1 删除parent_id 为这个id的数据+直接删除

            Category category = iCategoryService.queryByID(id);
            Integer parentId = category.getParentId();
            System.out.println("parentId---->"+parentId);
            if(parentId == -1){
                System.out.println("进入到if条件中");
                int i = iCategoryService.removeByParentId(id);
            }
            iCategoryService.remove(id);
            result = new ResultResponse<>(200,"删除类别成功!");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"删除类别失败!");
        }
        return result ;
    }

    Category category = new Category();
    @PutMapping("/modify")
    @ApiOperation(value = "修改类别")
    public ResultResponse<Void> modify(@RequestBody CategoryVo categoryVo){

        ResultResponse<Void> result = null;
        category.setId(categoryVo.getId());
        category.setCategoryCode(categoryVo.getCategoryCode());
        category.setType(categoryVo.getType());
        category.setParentId(categoryVo.getParentCategory().getId());
        try {
            iCategoryService.modify(category);
            result = new ResultResponse<>(200,"修改类别成功!");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"修改类别失败!");
        }
        return result ;
    }


    @GetMapping("/allList")
    @ApiOperation(value = "查询全部类别")
    public ResultResponse<List<Category>> allList(){
        ResultResponse<List<Category>> result = null;

        try {
            List<Category> lists = iCategoryService.findAll();
            result = new ResultResponse<>(200,"查询全部类别成功!",lists);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询全部类别失败!");
        }
        return result ;
    }

    Map<String, String> map = new HashMap<>();
    @GetMapping("/getMap")
    @ApiOperation(value = "查询集合")
    public ResultResponse<Map> getMap(){

        ResultResponse<Map> result = null;

        try {
            Category code = iCategoryService.findParentCode();
            map.put("-1", code.getCategoryCode());
            System.out.println(map);
            List<Category> parentID = iCategoryService.findAllParentID();
            for(int i=0;i<parentID.size();i++){
               Category category = iCategoryService.findCategoryByID(parentID.get(i).getId());
               Category code3 = iCategoryService.findParentCodeByID(parentID.get(i).getId());
               if(code3 ==null){
                   map.put(category.getCategoryCode(), category.getCategoryCode()+"00");
               }else{
                   map.put(category.getCategoryCode(), code3.getCategoryCode());
               }
            }
            result = new ResultResponse<>(200,"查询集合成功!",map);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询集合失败!");
        }
        return result ;
    }

}
